<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @itemClick="itemClick" ref="nav" />
    <scroll ref="scroll" class="content" :pullUpLoad="true" :probeType="3" @scroll="detailScroll">
      <detail-swiper :topImages="topImages" />
      <detail-base-info :goods="goods" />
      <detail-shop-info :shop="shop" />
      <detail-goods-info :detailInfo="detailInfo" @imageLoad="imageLoad" />
      <detail-param-info :paramInfo="paramInfo" ref="param" />
      <detail-comment-info :commentInfo="commentInfo" ref="comment" />
      <goods-list :goods="recommends" ref="recommend" />
    </scroll>
    <back-top @click.native="goTop" v-show="isShowBackTop" />
    <detail-bottom-bar @addToCart="addToCart" />
  </div>
</template>

<script>
import {
  getDetail,
  getRecommend,
  Goods,
  Shop,
  GoodsParam,
} from "network/detail";
import { itemListenerMixin,backTopMixin } from "common/mixin";
import { debounce } from "common/utils";

import Scroll from "components/common/scroll/Scroll";

import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import DetailBottomBar from './childComps/DetailBottomBar'

import GoodsList from "components/content/goodsList/GoodsList";

export default {
  name: "Detail",
  data() {
    return {
      iid: "",
      topImages: [],
      goods: {},
      shop: {},
      scroll: null,
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      topYs: [],
      getTops: null,
      currentIndex: 0,
    };
  },
  mixins: [itemListenerMixin,backTopMixin],
  components: {
    Scroll,
    GoodsList,
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar
  },
  created() {
    this.iid = this.$route.params.iid;
    /**
     * 网络请求
     */
    getDetail(this.iid).then((res) => {
      const { itemInfo, columns, shopInfo, detailInfo, itemParams, rate } =
        res.result;
      console.log(res);
      // 详情轮播图数据
      this.topImages = res.result.itemInfo.topImages;

      // 商品信息
      this.goods = new Goods(itemInfo, columns, shopInfo.services);

      // 店铺信息
      this.shop = new Shop(shopInfo);

      // 商品的详情数据
      this.detailInfo = detailInfo;

      // 商品参数数据
      this.paramInfo = new GoodsParam(itemParams.info, itemParams.rule);

      // 商品评论信息
      if (rate.cRate !== 0) {
        this.commentInfo = rate.list[0];
      }
    });

    getRecommend().then((res) => {
      this.recommends = res.data.list;
    });

    // 获取参数，评论，推荐的offsetTop

    this.getTops = debounce(() => {
      this.topYs = [];
      this.topYs.push(0);
      this.topYs.push(this.$refs.param?.$el?.offsetTop || 0);
      this.topYs.push(this.$refs.comment?.$el?.offsetTop || 0);
      this.topYs.push(this.$refs.recommend?.$el?.offsetTop || 0);
      this.topYs.push(Number.MAX_VALUE);
    }, 100);
  },
  mounted() {},
  updated() {},
  methods: {
    imageLoad() {
      this.$refs.scroll.refresh();
      this.getTops();
    },
    itemClick(index) {
      this.$refs.scroll.scrollTo(0, -this.topYs[index], 200);
      console.log(this.topYs);
      console.log(index);
    },
    detailScroll(position) {
      const { y } = position;
      let positionY = -y;
      let length = this.topYs.length;
      // console.log(positionY);
      for (let i = 0; i < length - 1; i++) {
        if (
          this.currentIndex !== i &&
          i < length - 1 &&
          positionY >= this.topYs[i] &&
          positionY < this.topYs[i + 1]
        ) {
          this.currentIndex = i;
          console.log(i); // 因为此时打印非常频繁，所以将当前index保存为一个变量，在两个不等的情况下才打印，就不会很频繁
          console.log(this.topYs[i]); // 此处的值是正值
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
      // console.log(y);

      // 添加回到顶部
      this.listenerBackTop(position)
    },
    addToCart(){
      let product = {}
      product.iid = this.iid // 添加商品iid
      product.image = this.topImages[0] // 添加商品的图片
      product.title = this.goods.title // 添加商品的标题
      product.desc = this.goods.desc // 添加商品的描述
      product.price = this.goods.realPrice //添加商品的价格

      // this.$store.commit('addCart',product)
      // console.log('添加商品')
      this.$store.dispatch('addCart',product).then(res=>{
        this.$toast.show(res,1500)
      })

    }
  },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.content {
  height: calc(100% - 44px - 49px);
  overflow: hidden;
  /* background-color: #fff; */
}
</style>